package action.back;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.Icd;
import models.Icdfl;
import models.Icdlb;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import utils.JsonUtils;


public class IcdAction extends BaseAction{
	
	public String index() {
		return "index";
	}
	
	public void getChildren() {
		String flidStr = request.getParameter("flid");
		Integer flid = 0;
		if(!StringUtils.isEmpty(flidStr))
			flid = Integer.parseInt(flidStr.substring(2));
		
		DetachedCriteria dc = DetachedCriteria.forClass(Icd.class);
		dc.add(Restrictions.eq("flid", flid));
		page = getCommonService().findPage(dc, start, limit);
		
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("items",JsonUtils.toMapList(page.getItems(), "icd"));
		jsonMap.put("total", page.getTotal());
		
		JSONObject jsonObject = JSONObject.fromObject(jsonMap);
		String jsonString = jsonObject.toString();
		renderJson(jsonString);
	}
	
	
	@SuppressWarnings("unchecked")
	public void getTree() {
		
		String type  = request.getParameter("type");
		String xtype = request.getParameter("xtype");
		
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
//		lb->fl->icd
//		1	1	B	中医疾病编码
//		2	2	D	ICD-10疾病编码
//		3	3	M	肿瘤形态学编码
//		4	4	S	ICD-9-CM3手术编码
//		5	5	Y	损伤中毒的外部原因
//		6	6	Z	中医病征编码
		if("".equals(type)|| type == null){
			List<Icdlb> lblist = getCommonService().findAll("from Icdlb");
			for(Icdlb icdlb : lblist){
				Map<String, Object> itemMap = new HashMap<String, Object>();
				itemMap.put("id", "lb" + icdlb.getId());
				itemMap.put("text", icdlb.getLb().toString());
				itemMap.put("leaf",false);
				
				itemMap.put("xtype",icdlb.getBm());
				itemMap.put("type", "lb");
				list.add(itemMap);
			}
		}
		
		else if("lb".equals(type) ){
			
			DetachedCriteria dc = DetachedCriteria.forClass(Icdfl.class);
			dc.add(Restrictions.or(Restrictions.isNull("sjid"), Restrictions.eq("sjid",0)));
			dc.add(Restrictions.eq("lb",xtype));
			
			List<Icdfl> flList = getCommonService().findByCriteria(dc);
			for(Icdfl icdfl : flList){
				Map<String, Object> itemMap = new HashMap<String, Object>();
				itemMap.put("id", "fl" + icdfl.getId());
				itemMap.put("text", icdfl.getMc());
				itemMap.put("leaf",false);
				
				itemMap.put("type", "fl");
				itemMap.put("xtype",icdfl.getLb());
				list.add(itemMap);
			}
		}
		else if("fl".equals(type) ){
			Integer parent_id = Integer.parseInt(request.getParameter("parent_id").substring(2));
			DetachedCriteria dc = DetachedCriteria.forClass(Icdfl.class);
			dc.add( Restrictions.eq("sjid",parent_id));
			dc.add(Restrictions.eq("lb",xtype));
			List<Icdfl> flList = getCommonService().findByCriteria(dc);
			for(Icdfl icdfl : flList){
				boolean isLeaf =  icdfl.getSubCount() == 0 ;
				Map<String, Object> itemMap = new HashMap<String, Object>();
				itemMap.put("id", "fl" + icdfl.getId());
				itemMap.put("text", icdfl.getMc());
				itemMap.put("leaf",isLeaf);
				
				itemMap.put("type", "fl");
				itemMap.put("xtype",icdfl.getLb());
				list.add(itemMap);
			}
		}
		JSONArray jsonObject = JSONArray.fromObject(list);
		String jsonString = jsonObject.toString();
		renderJson(jsonString);
	}
}
